package com.taxshare.operation.service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.pager.PageHelp;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.mybatis.utils.DozerBeanUtil;
import com.taxshare.operation.entity.ApiConsoleEntity;
import com.taxshare.operation.mapper.ApiConsoleMapper;
import com.taxshare.operation.view.vo.ApiConsoleReqVO;
import com.taxshare.operation.view.vo.VisualRangeReqVO;
import com.taxshare.response.beans.Response;
import com.taxshare.tools.util.UUIDUitl;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class ApiConsoleService extends ServiceImpl<ApiConsoleMapper, ApiConsoleEntity> {

	/**
	 * @Title: queryPageList
	 * @Description: (接口列表)
	 * @param @param query
	 * @param @return 参数
	 * @return Response<IPage<ApiConsoleReqVO>> 返回类型
	 * @throws
	 */
	public Response<IPage<ApiConsoleReqVO>> queryPageList(PageRequest<ApiConsoleReqVO> query) {
		QueryWrapper<ApiConsoleEntity> queryWrapper = new QueryWrapper<>();
		String customerName = query.getInfo().getCustomerName();
		if (StringUtils.isNotEmpty(customerName)) {
			queryWrapper.like("customer_name", customerName);
		}
		queryWrapper.orderByDesc("create_time");
		IPage<ApiConsoleReqVO> result = PageHelp.siamplePage(this, queryWrapper, query.getPage(), ApiConsoleReqVO.class);
		return Response.success(result);
	}

	/**
	 * @Title: showApiConsole
	 * @Description: (单条接口权限数据)
	 * @param @param uuid
	 * @param @return 参数
	 * @return Response 返回类型
	 * @throws
	 */
	public Response showApiConsole(String uuid) {
		ApiConsoleEntity result = this.getById(uuid);
		VisualRangeReqVO visualRangeReqVO = new VisualRangeReqVO();
		visualRangeReqVO.setUuid(result.getUuid());
		visualRangeReqVO.setVisualRange(result.getVisualRange());
		if(StringUtils.isNotEmpty(result.getVisualRangeList())){
			String str[] = result.getVisualRangeList().split(",");
			visualRangeReqVO.setVisualRangeList(Arrays.asList(str));
		}
		return Response.success(visualRangeReqVO);
	}

	/**
	 * @Title: add
	 * @Description: (新增接口)
	 * @param @param commonCodeInfoDTO
	 * @param @return 参数
	 * @return Response 返回类型
	 * @throws
	 */
	public Response saveApi(ApiConsoleReqVO apiConsoleReqVO) {
		ApiConsoleEntity apiConsoleEntity = DozerBeanUtil.map(apiConsoleReqVO, ApiConsoleEntity.class);
		apiConsoleEntity.setUuid(UUIDUitl.UUID32(false));
		apiConsoleEntity.setAppkey(UUIDUitl.UUID32(false));
		apiConsoleEntity.setSecurity(UUIDUitl.UUID32(false));
		apiConsoleEntity.setCreateTime(new Date());
		apiConsoleEntity.setUpdateTime(new Date());
		this.save(apiConsoleEntity);
		return Response.success();
	}

	/**
	 * @Title: udpateApiInfo
	 * @Description: (接口修改)
	 * @param @param ApiConsoleReqVO
	 * @param @return 参数
	 * @return Response 返回类型
	 * @throws
	 */
	public Response udpateApiInfo(VisualRangeReqVO visualRangeReqVO) {
		ApiConsoleEntity apiConsoleEntity = this.getById(visualRangeReqVO.getUuid());
		apiConsoleEntity.setVisualRange(visualRangeReqVO.getVisualRange());
		apiConsoleEntity.setUpdateTime(new Date());
		if ("1".equals(visualRangeReqVO.getVisualRange())) {
			apiConsoleEntity.setVisualRangeList("");
		} else {
			List<String> vrList = visualRangeReqVO.getVisualRangeList();
			String visualRangeList = String.join(",", vrList);
			apiConsoleEntity.setVisualRangeList(visualRangeList);
		}
		return Response.success(this.updateById(apiConsoleEntity));
	}

	/**
	 * @Title: delete
	 * @Description: (接口删除)
	 * @param @param ids
	 * @param @return 参数
	 * @return Response 返回类型
	 * @throws
	 */
	public Response deleteApiInfo(String uuid) {
		return Response.success(this.removeById(uuid));
	}
}
